package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ProfileDataBean;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ProfileAdapter extends RecyclerAdapter<ProfileAdapter.ViewHolder> {

    private List<ProfileDataBean> mList;
    private Context context;

    public ProfileAdapter(List<ProfileDataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_profile_list, componentContainer, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ViewHolder viewHolder = holder;
        if (mList != null) {
            viewHolder.img.setWidth(300);
            viewHolder.img.setImageAndDecodeBounds(mList.get(position).getImg());
            if (mList.get(position).getName()==null){
                viewHolder.name.setVisibility(Component.HIDE);
            }else {
                viewHolder.img.setCornerRadius(1000);
                viewHolder.img.setWidth(260);
            }
            viewHolder.name.setText(mList.get(position).getName());
        }

    }


    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int positon) {
        return positon;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {

        private Image img;
        private Text name;

        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            img = (Image) itemComponent.findComponentById(ResourceTable.Id_img);
            name = (Text) itemComponent.findComponentById(ResourceTable.Id_name);
        }
    }
}
